<template>
  <div class="app-container">

    <el-form :model="queryForm" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="字段名称">
        <el-input v-model="queryForm.resultName" placeholder="请输入字段名称" clearable />
      </el-form-item>
      <el-form-item label="数据库名称">
        <el-input v-model="queryForm.databaseName" placeholder="请输入数据库名称" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery" icon="el-icon-search" size="mini">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

     <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['contract:ice-result:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['contract:ice-result:add']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['contract:ice-result:add']"
        >删除</el-button>
      </el-col>
       <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>

    </el-row>

    <el-table :data="dataList" @selection-change="handleSelectionChange" fit border width="100%">
      <el-table-column type="selection" width="55" align="center" fixed />
      <el-table-column label="ID" align="center" prop="id" fixed width="100" show-overflow-tooltip />
      <el-table-column label="字段名称" prop="resultName" align="center" show-overflow-tooltip />
      <el-table-column label="数据库名称" prop="databaseName" align="center" show-overflow-tooltip />
      <el-table-column label="单位" align="center" prop="calUnit" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cal_unit" :value="scope.row.calUnit" />
        </template>
      </el-table-column>
      <el-table-column label="备注" prop="remark" align="center" show-overflow-tooltip />
      <el-table-column label="创建人" prop="createBy" align="center" show-overflow-tooltip />
      <el-table-column label="创建时间" prop="createTime" align="center" show-overflow-tooltip />
      <el-table-column label="更新人" prop="updateBy" align="center" show-overflow-tooltip />
      <el-table-column label="更新时间" prop="updateTime" align="center" show-overflow-tooltip />
    </el-table>

     <pagination
       v-show="total>0"
       :total="total"
       :page.sync="queryForm.pageNum"
       :limit.sync="queryForm.pageSize"
       @pagination="getList"
     />

    <el-dialog :title="title" :visible.sync="open" width="350px" append-to-body :close-on-click-modal="false" @closed="reset">
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-form-item label="字段名称" prop="resultName">
            <el-input v-model="form.resultName" clearable style="width: 200px"/>
          </el-form-item>
          <el-form-item label="数据库名称" prop="databaseName">
            <el-input v-model="form.databaseName" clearable style="width: 200px"/>
          </el-form-item>

          <el-form-item label="计费单位" prop="calUnit">
            <el-select v-model="form.calUnit" placeholder="请选择计费单位" style="width: 200px" filterable>
              <el-option
                v-for="dict in dict.type.cal_unit"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" clearable style="width: 200px"/>
          </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>

     </el-dialog>

  </div>
</template>

<script>
import {delResult, getOneResult, listIceResult, saveResult, updateResult} from "@/api/base/result";



export default {
  name: "IceResult",
  dicts:['cal_unit'],
  data(){
    return{
      showSearch:true,
      dataList: [],
      total: 0,
      queryForm: {
        pageNum: 1,
        pageSize: 10,
      },
      form: {
        id: null,
        resultName: '',
        confName: '',
        databaseName: '',
        remark: '',
      },
      single: true,
      multiple: true,

      title: '',
      open: false,

      rules: {

        resultName: [
          {required: true, message: "计费字段名称不能为空", trigger: "blur"}
        ],

        databaseName: [
          {required: true, message: "数据库名称不能为空", trigger: "blur"}
        ],

        calUnit:[
          {required: true, message: "计费单位不能为空", trigger: "blur"}
        ]

      }

    }
  },
  created() {
    this.handleQuery()
  },
  methods:{

    handleQuery(){
      this.queryForm.pageNum = 1
      this.getList()
    },
    
    resetQuery(){
      this.queryForm= {
        pageNum: 1,
        pageSize: 10,
      };
      this.handleQuery();
    },

    getList(){
      listIceResult(this.queryForm).then(res => {
        console.log(res)
        this.total = res.total
        this.dataList = res.rows

      })
    },

    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateResult(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            saveResult(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    reset() {
      this.form = {
        id: null,
        resultName: '',

        databaseName: '',

        remark: '',
      };


    },


    handleAdd() {

      this.open = true;
      this.title = "新增计费字段";
    },
    handleUpdate(){

      const id =  this.ids
      getOneResult(id).then(response => {
        console.log(response)
        this.form = response.data;
        this.open = true;
        this.title = "修改计费字段";
      });
    },
    handleDelete(){
      const ids = this.ids;
      this.$modal.confirm('是否确认删除计费字段编号为"' + ids + '"的数据项？').then(function() {
        return delResult(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
}
</script>

<style scoped>

</style>
